package com.digitalchina.lxnsy.activity.ecardConsult;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessFile;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.FileVO;
import com.digitalchina.lxnsy.loader.GlideImageLoader;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.okhttputils.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/3/14 17:53
 * 修改备注：
 * ================================================
 */
public class EcardReplyConsultActivity extends BaseActivity {

    @Bind(R.id.consult_issue_et_issue)
    EditText consultIssueEtIssue;
    @Bind(R.id.consult_issue_btn_ask)
    Button consultIssueBtnAsk;

    // 图片
    public static final int IMAGE_ITEM_ADD = -1;
    public static final int REQUEST_CODE_SELECT = 100;
    public static final int REQUEST_CODE_PREVIEW = 101;
    @Bind(R.id.sdv_img)
    SimpleDraweeView sdvImg;


    private List<String> filePath = new ArrayList<>(); // 图片地址
    private String img = ""; // 图片

    private String id = "";

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_reply_ecard_consult);
        setTitle("咨询回复");
    }


    @Override
    public void initMember() {

        id = (String) getIntent().getSerializableExtra("id");

        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);  //显示拍照按钮
        imagePicker.setMultiMode(false);    //是否多选模式
        imagePicker.setCrop(false);        //允许裁剪（单选才有效）

    }

    @Override
    public void addListener() {


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            //添加图片返回
            if (data != null && requestCode == REQUEST_CODE_SELECT) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                if (images != null && images.size() > 0) {
                    showDialog("正在上传图片...");
                    BusinessFile.imageCompressAndUpload(this, images.get(0).path, mHandler);
                }
            }
        }
    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {

                switch (msg.what) {
                    case MSG.MSG_FILE_UPLOAD_SUCCESS: // 上传图片
                        dismissDialog();
                        FileVO fileVO = FastJsonUtil.getBean(msg.obj.toString(), "body", "fileStorage", FileVO.class);
                        img = fileVO.getName();
                        FrescoUtil.showImageSmall(img, sdvImg);
                        break;
                    case MSG.MSG_FILE_UPLOAD_FIELD:
                        dismissDialog();
                        showToast(msg.obj.toString() + "");
                        break;
                    case MSG_S_0:
                        dismissDialog();
                        new MaterialDialog.Builder(EcardReplyConsultActivity.this)
                                .title("提示")
                                .content("回复成功。")
                                .positiveText("确定")
                                .cancelable(false)
                                .canceledOnTouchOutside(false)
                                .onPositive(new MaterialDialog.SingleButtonCallback() {
                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                        setResult(RESULT_OK);
                                        finish();
                                    }
                                })
                                .show();
                        break;
                    case MSG_F_0:
                        dismissDialog();
                        showToast(msg.obj.toString() + "");
                        break;
                }
                return false;
            }
        });
    }


    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    public void reply() {
        HttpParams params = new HttpParams();
        params.put("img", img);
        params.put("expertConsultId", id);
        params.put("replyContent", consultIssueEtIssue.getText().toString());


        OkHttpUtil.post(activity, URL.URL_REPLY_CONSULT, "", params, mHandler,
                MSG_S_0, MSG_F_0);
    }

    @OnClick({R.id.sdv_img, R.id.consult_issue_btn_ask})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sdv_img:
                Intent intent = new Intent(activity, ImageGridActivity.class);
                startActivityForResult(intent, REQUEST_CODE_SELECT);
                break;
            case R.id.consult_issue_btn_ask:
                String issue = consultIssueEtIssue.getText().toString();
                if (StringUtil.isStrEmpty(issue) || issue.length() < 4) {
                    showToast("请填写不低于4个字的问题描述");
                    return;
                }

                showDialog("正在提交...");
                reply();
                break;
        }
    }
}
